<template>
	<view class="login-body">
		<image src="../../static/image/login/logo.png" mode="widthFix" class="login-logo"></image>
		<!-- 登录表单 -->
		<view class="login-form">
			<view class="form-item">
				<view class="form-label">
					<img src="../../static/image/login/account.png"  class="form-icon">
				</view>
				<view class="form-content">
					<input type="number" pattern="\d*" @blur="rules" placeholder="请输入手机号" v-model="form.mobile" class="form-input"/>
				</view>
			</view>
			<view class="form-item">
				<view class="form-label">
					<img src="../../static/image/login/password.png"  class="form-icon">
				</view>
				<view class="form-content">
					<input type="password" placeholder="请输入密码" v-model="form.password" class="form-input"/>
				</view>
			</view>
		</view>
		<!-- 按钮 -->
		<view class="login-btn" @tap="submit()">
			<lg-btn>确认登录</lg-btn>
		</view>
		<view class="login-btn wx-btn" @tap="oauth()">
			<lg-btn bg="#55a947"><i class="icon">&#xe632;</i>微信登录</lg-btn>
		</view>
		<view class="other-btn">
			<view @tap="forget()">忘记密码？</view>
			<view @tap="reg()">立即注册</view>
		</view>
	</view>
</template>

<script>
	import lgBtn from '../../components/unit/button.vue';
	
	export default{
		components:{ lgBtn },
		data(){
			return {
				form :{
					mobile :'',
					password : ''
				}
			}
		},
		onLoad(e) {
			var code = this.getUrlParam("code");
			console.log(code)
			if(code) {
				this.oauthLogin(code);
			}
		},
		methods:{
			// 验证手机号
			rules() {
				  if(!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(this.form.mobile))){ 
					   this.toast('手机号格式不正确')
				    } 
			},
			
			//授权登录
			oauth(){
				var appId = 'wxed3fff9ad2e366cf';
				var url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appId + "&redirect_uri=" + location.href.split('#')[0] + "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
				var code = this.getUrlParam("code");
				if(!code) {
					window.location = url;
				}else{
					this.oauthLogin(code);
				}
			},
			//授权登录
			oauthLogin(code){
				this.ajax('wx/index',{
					data:{code : code},
					success:(res) =>{
						if(res.code == 1) {
							//登录成功
							this.toast(res.msg);
							setTimeout(()=>{
								uni.switchTab({
									url:"/pages/user/index"
								})
							},1000)
						}
						if(res.code == 2) {
							//登录成功，新用户注册
							//alert('登录成功：'+res.msg);
							uni.navigateTo({
								url:'/pages/login/wx?wx_info='+res.msg,
							});
						}
						if(res.code == 0) {
							//登录失败
							this.toast(res.msg);
							setTimeout(()=>{
								uni.reLaunch({
									url:'/pages/login/login'
								})
							},1000)
							//alert('登录失败：'+res.msg);
						}
					}
				});
			},
			//获取CODE
			getUrlParam(name) {
			  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
			  var r = window.location.search.substr(1).match(reg);
			  if (r != null) return unescape(r[2]);
			  return null;
			},
			//去注册
			reg(){
				uni.navigateTo({
					url:"/pages/login/reg"
				})
			},
			//登录
			submit(){
				if(!this.form.mobile)return this.toast('请输入手机号');
				if(!this.form.password)return this.toast('请输入密码');
				if(!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(this.form.mobile))) return this.toast('手机号格式不正确');
				this.ajax('login/login',{
					data:this.form,
					success:(res) =>{
						this.toast(res.msg);
						if(res.code) {
							setTimeout(()=>{
								uni.switchTab({
									url:"/pages/user/index"
								})
							},1000)
						}
					}
				});
			},
			//忘记密码
			forget(){
				uni.navigateTo({
					url:'/pages/login/forget'
				})
			},
		}
	}
</script>

<style>
	@import url("../../static/css/login/index.css");
</style>
